new Handle:g_DissolvePlayer  = INVALID_HANDLE
new Handle:g_DissolveType  = INVALID_HANDLE
new Handle:g_DissolveDelay  = INVALID_HANDLE

//---------------------------------------------//
// If dissolved enabled we doing it right here //
//---------------------------------------------//
public Action:DissolvePlayer(client)
{
	if (GetConVarInt(g_DissolvePlayer) == 1)
		{
			// Get time after ragdolls should be dissolved
			new Float:m_Delay2 = GetConVarFloat(g_DissolveDelay)
			// Should be above 0 seconds, because Timer accept only 0.1 seconds and above
			if (m_Delay2>0.0)
			{
				CreateTimer(m_Delay2, DO_DissolvePlayer, client)
			}
			// If time is 0 then we go here
			else
			{
				DO_DissolvePlayer(INVALID_HANDLE, client)
			}
		}
}

//----------------------------------//
// Dissolve Player/Client Function //
//----------------------------------//
public Action:DO_DissolvePlayer(Handle:timer, any:client)
{
	// Check if Player/Client is Valid
	if ((client <= 0) || !IsValidEntity(client)){
		return Plugin_Handled
	}

	new m_Ragdoll
	new bool:IsPlayer = true
	new String:classname[32]
	GetEdictClassname(client, classname, sizeof(classname))
	
	// Get the ragdoll of the dead Player/Client
	if(StrEqual(classname, "player", false))
	{
		m_Ragdoll = GetEntPropEnt(client, Prop_Send, "m_hRagdoll")
		// Check if Ragdoll is Valid
		if (m_Ragdoll <= 0)
		{
			return Plugin_Handled
		}
	}
	else
	{
		IsPlayer = false
	}

	new String:dname[32], String:dtype[32]
	Format(dname, sizeof(dname), "dis_%d", client)
	Format(dtype, sizeof(dtype), "%d", GetConVarInt(g_DissolveType))
	
	// Create env_entity_dissolver
	new ent = CreateEntityByName("env_entity_dissolver")
	
	// Check if env_entity_dissolver is Valid
	if (ent>0 && IsValidEntity(ent))
	{
		if(IsPlayer)
		{
			DispatchKeyValue(m_Ragdoll, "targetname", dname)
		} 
		else 
		{
			DispatchKeyValue(client, "targetname", dname)
		}
		DispatchKeyValue(ent, "dissolvetype", dtype)
		DispatchKeyValue(ent, "target", dname)
		AcceptEntityInput(ent, "Dissolve")
		AcceptEntityInput(ent, "kill")
	}
	return Plugin_Handled
}